package org.budo.jdbc.driver;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import org.budo.jdbc.driver.exception.BudoJdbcDriverNotSupportedException;
import org.budo.jdbc.driver.parser.SqlStatementParser;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @author li
 */
@Slf4j
@Getter
@Setter
@Accessors(fluent = true)
public abstract class AbstractBudoJdbcConnection implements Connection {
    private SqlStatementParser statementParser;

    private boolean autoCommit = true;

    private boolean readOnly = false;

    private boolean closed = false;

    private JdbcUrl jdbcUrl;

    @Override
    public boolean isValid(int timeout) throws SQLException {
        log.debug("#66 isValid return true, timeout={}, this={}", timeout, this);
        return true;
    }

    @Override
    public int getTransactionIsolation() throws SQLException {
        return Connection.TRANSACTION_NONE;
    }

    @Override
    public int getHoldability() throws SQLException {
        return ResultSet.CLOSE_CURSORS_AT_COMMIT;
    }

    /**
     * druid close/recycle connection 时会调用
     */
    @Override
    public void clearWarnings() throws SQLException {
        // log.info("#72 clearWarnings, this=" + this);
    }

    @Override
    public void setCatalog(String catalog) throws SQLException {
        this.jdbcUrl().setCatalog(catalog);
    }

    @Override
    public boolean isClosed() throws SQLException {
        return this.closed;
    }

    @Override
    public void close() throws SQLException {
        this.closed = true;
    }

    @Override
    public String getCatalog() throws SQLException {
        return this.jdbcUrl().getCatalog();
    }

    @Override
    public void setReadOnly(boolean readOnly) throws SQLException {
        this.readOnly = readOnly;
    }

    @Override
    public boolean isReadOnly() throws SQLException {
        return this.readOnly;
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {
        this.autoCommit = autoCommit;
    }

    @Override
    public boolean getAutoCommit() throws SQLException {
        return this.autoCommit;
    }

    @Override
    public void commit() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException("#53 this=" + this);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        log.warn("#83 prepareStatement(String, int, int, int) is not supported, sql=" + sql + ", resultSetType=" + resultSetType + ", resultSetConcurrency=" + resultSetConcurrency + ", resultSetHoldability=" + resultSetHoldability);

        return this.prepareStatement(sql);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        log.warn("#77 prepareStatement(sql, autoGeneratedKeys) is not supported, sql=" + sql + ", autoGeneratedKeys=" + autoGeneratedKeys + ", this=" + this);

        return this.prepareStatement(sql);
    }

    public String nativeSQL(String sql) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public String getClientInfo(String name) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Properties getClientInfo() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void setClientInfo(String name, String value) throws SQLClientInfoException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void setClientInfo(Properties properties) throws SQLClientInfoException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public CallableStatement prepareCall(String sql) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void rollback() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException("#107 this=" + this);
    }

    public void setTransactionIsolation(int level) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public SQLWarning getWarnings() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Map<String, Class<?>> getTypeMap() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void setHoldability(int holdability) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Savepoint setSavepoint() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Savepoint setSavepoint(String name) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void rollback(Savepoint savepoint) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Clob createClob() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Blob createBlob() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public NClob createNClob() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public SQLXML createSQLXML() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void setSchema(String schema) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public String getSchema() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void abort(Executor executor) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public int getNetworkTimeout() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }
}